package com.cpr.shop.result;

import com.cpr.shop.code.ICode;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

import static com.cpr.shop.code.SuccessCode.SUCCESS_CODE;

/**
 * @Author CPR
 * @Create 2025/1/23 22:21
 * @Version 1.0
 * @Description
 */
@Data
@Builder
public final class Result<T> implements Serializable {
    private static final long serialVersionUID = 6565465132252626416L;

    private Integer code;
    private String message;
    private T data;

    private static <T> Result<T> build(Integer code,String message){
        return Result.<T>builder()
                .code(code)
                .message(message)
                .build();
    }

    private static <T> Result<T> build(Integer code,String message,T body){
        return Result.<T>builder()
                .code(code)
                .message(message)
                .data(body)
                .build();
    }

    /**
     * 成功结果
     */
    public static <T> Result<T> success(T body){
        Result<T> build = build(
                SUCCESS_CODE.getCode(),
                SUCCESS_CODE.getMessage(),
                body
        );
        return build;
    }

    /**
     * 失败结果
     */
    public static <T> Result<T> fail(ICode codeEnum){
        Result<T> build = build(
                codeEnum.getCode(),
                codeEnum.getMessage()
        );

        return build;
    }
}
